﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EZCRS.HCMS.Entity;
using EZCRS.HCMS.Data;
using System.Data;
using System.Data.SqlClient;

namespace EZCRS.HCMS.SqlClient
{
    public class sql_AdminUser:MainBase
    {
        private string ErrMsg = "";
        private string SqlStr = "";

        /// <summary>
        /// 错误信息
        /// </summary>
        public string ErrMsgs
        {
            get { return ErrMsg; }
        }

        #region 处理登录

        public bool CheckUser(string uname, string upass, string Ip)
        {
            SqlStr = "update hfg_admuser set lastip='" + Ip + "',lastdate=getdate(),logcount=logcount+1 where logname='" + uname + "' and logpassword='" + upass + "' and admstatus=1";
            if (ExecSql.SqlExecNoquery(SqlStr) > 0)
                return true;
            else
                return false;
        }

        public DataTable GetLoginUser(string uname)
        {
            SqlStr = "select hfg_admrole.*,hfg_admuser.admid,hfg_admuser.orgid as aorgid,hfg_admuser.orgtype as aorgtype from hfg_admrole,hfg_admuser  where hfg_admrole.rolid = hfg_admuser.rolid and hfg_admuser.logname='" + uname + "' ";
            return ExecSql.GetDataSet(SqlStr).Tables[0];
        }

        public bool CheckUserThree(string AdmId,out string rolestr)
        {
            rolestr = "";
            SqlStr = "select languagetype from hfg_admuser_setup where admid=" + AdmId;
            DataTable dt1 = ExecSql.GetDataSet(SqlStr).Tables[0];
            if (dt1.Rows.Count > 0)
            {
                rolestr = dt1.Rows[0]["languagetype"].ToString();
                return true;
            }
            else
                return false;
        }

        

       
        #endregion

        #region 后台用户相关；添加、删除、修改、权限分配

        /// <summary>
        /// 生成随即密码 5 位数字
        /// </summary>
        /// <returns></returns>
        public static string CreatePassword()
        {
            Random r = new Random();
            int rl = r.Next(10000, 99999);
            return rl.ToString();
        }


        /// <summary>
        /// 返回用户名是否重复,如果存在，则返回true
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public bool User_Have(string UserName)
        {
            DataTable dt = ExecSql.GetDataSet("select * from hfg_admuser where logname='" + UserName + "'").Tables[0];
            if (dt.Rows.Count > 0)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 添加或修改管理用户
        /// </summary>
        /// <param name="AdmId">管理员编号  0=为新增管理员 其他为修改</param>
        /// <param name="OrgId">集团编号</param>
        /// <param name="Orgtype">集团类型</param>
        /// <param name="RolId">角色编号</param>
        /// <param name="LogName">登录名</param>
        /// <param name="LogPassword">登录密码</param>
        /// <param name="Username">用户真实姓名</param>
        /// <param name="UserEmail">电子邮箱</param>
        /// <param name="UsrContact">联系方式</param>
        /// <param name="AdmStatus">状态 0=正常 1=暂停</param>
        /// <param name="InUser">录入人</param>
        /// <returns></returns>
        public bool User_Add(int AdmId, int OrgId, int Orgtype, string RolId, string LogName, string LogPassword, string Username, string UserEmail, string UsrContact, string AdmStatus, string InUser)
        {
            if (AdmId == 0)
                SqlStr = "insert into hfg_admuser(orgid,orgtype,rolid,logname,logpassword,usrname,usremail,usrcontact,admstatus,indate,inuser) values(@orgid,@orgtype,@rolid,@logname,@logpassword,@usrname,@usremail,@usrcontact,@admstatus,'" + DateTime.Now.ToString() + "',@inuser)";
            else
                SqlStr = "update hfg_admuser set rolid=@rolid,usrname=@usrname,usremail=@usremail,usrcontact=@usrcontact,admstatus=@admstatus where admid=" + AdmId.ToString() + "";

            SqlParameter[] pam = new SqlParameter[10];
            pam[0] = new SqlParameter("@orgid", OrgId);
            pam[1] = new SqlParameter("@orgtype", Orgtype);
            pam[2] = new SqlParameter("@rolid", RolId);
            pam[3] = new SqlParameter("@logname", LogName);
            pam[4] = new SqlParameter("@logpassword", LogPassword);
            pam[5] = new SqlParameter("@usrname", Username);
            pam[6] = new SqlParameter("@usremail", UserEmail);
            pam[7] = new SqlParameter("@usrcontact", UsrContact);
            pam[8] = new SqlParameter("@admstatus", AdmStatus);
            pam[9] = new SqlParameter("@inuser", InUser);

            if (ExecSql.SqlExecNoquery(SqlStr, pam) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 密码初始化
        /// </summary>
        /// <param name="UId">管理员id</param>
        /// <returns></returns>
        public string User_InitPass(string UId,string Pass)
        {
            if (ExecSql.SqlExecNoquery("update hfg_admuser set logpassword='" + Pass + "' where admid=" + UId + "")>0)
                return Pass;
            else
                return "";
        }
        /// <summary>
        /// 管理员列表
        /// </summary>
        /// <param name="OrgId">组织编号</param>
        /// <param name="OrgType">组织类型 0=集团  1=酒店 3=hubs1</param>
        /// <returns></returns>
        public DataTable User_List(string OrgId, string OrgType)
        {
            if (OrgType == "3")
                SqlStr = "select * from hfg_admuser where orgtype=3";
            else if (OrgType == "0")
                SqlStr = "select * from hfg_admuser where orgid=" + OrgId + " and orgtype=0 ";
            else
                SqlStr = "select * from hfg_admuser where orgid=" + OrgId + " and orgtype=1 ";
            DataTable dt = ExecSql.GetDataSet(SqlStr).Tables[0];
            if (dt != null)
                return dt;
            else
                return null;
        }

        /// <summary>
        /// 删除管理用户
        /// </summary>
        /// <param name="AId">用户id</param>
        /// <returns></returns>
        public bool User_Del(string AId)
        {
            SqlStr = "delete from hfg_admuser where admid=" + AId + "";
            if (ExecSql.SqlExecNoquery(SqlStr)>0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 返回一条管理员信息
        /// </summary>
        /// <param name="UId">管理员id</param>
        /// <returns></returns>
        public DataRow User_Item(int UId, int GId)
        {
            return ExecSql.GetDataSet("select * from hfg_admuser where admid=" + UId.ToString() + " and orgid=" + GId.ToString()).Tables[0].Rows[0];
        }
        public DataRow User_Item(string UName)
        {
            try
            {
                return ExecSql.GetDataSet("select * from hfg_admuser where logname='" + UName + "'").Tables[0].Rows[0];
            }
            catch { return null; }
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="UserId">用户id</param>
        /// <param name="OldPassWord">旧密码</param>
        /// <param name="NewPassWord">新密码</param>
        /// <returns></returns>
        public bool User_Pass(string UserId, string OldPassWord, string NewPassWord)
        {
            if (ExecSql.SqlExecNoquery("update hfg_admuser set logpassword='" + NewPassWord + "' where admid=" + UserId + " and logpassword='" + OldPassWord + "'")>0)
                return true;
            else
                return false;
        }
        #endregion

        #region
        #endregion

        #region 角色相关 添加、删除、修改
        /// <summary>
        /// 添加或修改角色
        /// </summary>
        /// <param name="RolId">角色id 0为添加</param>
        /// <param name="RolName">角色名称</param>
        /// <param name="ModuleId">模块编号</param>
        /// <param name="HotelList">酒店列表</param>
        /// <param name="RolType">模块类别</param>
        /// <param name="RolComment">描述</param>
        /// <returns></returns>
        public bool Role_Add(int RolId, string RolName, string ModuleId, string HotelList, string GroupList, string RolType, string RolComment, string OrgId, string OrgType)
        {
            if (RolId == 0)
                SqlStr = "insert into hfg_admrole(orgid,orgtype,rolname,rolmodules,rolhotel,rolgroup,rolcomment,roltype) values(" + OrgId + "," + OrgType + ",@rolname,@rolmodules,@rolhotel,@rolgroup,@rolcomment," + RolType + ")";
            else
                SqlStr = "update hfg_admrole set orgtype=" + OrgType + ",orgid=" + OrgId + ",rolname=@rolname,rolmodules=@rolmodules,rolhotel=@rolhotel,rolgroup=@rolgroup,rolcomment=@rolcomment,roltype=" + RolType + " where rolid=" + RolId.ToString() + "";

            SqlParameter[] pam = new SqlParameter[5];
            pam[0] = new SqlParameter("@rolname", RolName);
            pam[1] = new SqlParameter("@rolmodules", ModuleId);
            pam[2] = new SqlParameter("@rolhotel", HotelList);
            pam[3] = new SqlParameter("@rolcomment", RolComment);
            pam[4] = new SqlParameter("@rolgroup", GroupList);

            if (ExecSql.SqlExecNoquery(SqlStr, pam) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 返回所有角色
        /// </summary>
        /// <param name="OrgId">组织编号 0 为超级管理员</param>
        /// <param name="OrgType">组织类型 0=集团 1=酒店 3=hubs1</param>
        /// <returns></returns>
        public DataTable Role_List(string OrgId, string OrgType)
        {
            if (OrgType == "3")
                SqlStr = "select * from hfg_admrole where orgid=0 and roltype=0 ";
            else
                SqlStr = "select * from hfg_admrole where orgid=" + OrgId + " and orgtype=0 and roltype=0";//

            DataTable dt = ExecSql.GetDataSet(SqlStr).Tables[0];

            if (dt != null)
                return dt;
            else
                return null;
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="RolId">角色id</param>
        /// <returns></returns>
        public bool Role_Del(int RolId)
        {
            //判断 是否 分配给管理员 如果分配，则不允许删除

            //删除
            if (ExecSql.SqlExecNoquery("delete from hfg_admrole where rolid=" + RolId.ToString() + " ") > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 返回一条角色信息
        /// </summary>
        /// <param name="RolId">角色id</param>
        /// <returns></returns>
        public DataRow Role_Item(string RolId, int GId)
        {
            if (Convert.ToInt32(RolId) <= 3)
                GId = 0;
            DataRow dr = ExecSql.GetDataSet("select * from hfg_admrole where rolid=" + RolId + " and orgid=" + GId.ToString()).Tables[0].Rows[0];
            if (dr != null)
                return dr;
            else
                return null;
        }
        /// <summary>
        /// 返回角色能够访问的酒店
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public string Role_Hotel(string RoleId)
        {
            DataRow dr = ExecSql.GetDataSet("select rolhotel from hfg_admrole where rolid=" + RoleId + "").Tables[0].Rows[0];
            if (dr != null)
                return dr["rolhotel"].ToString();
            else
                return "";
        }

        /// <summary>
        /// 返回角色能访问的集团
        /// </summary>
        /// <param name="RolId"></param>
        /// <returns></returns>
        public string Role_Group(string RolId)
        {
            DataRow dr = ExecSql.GetDataSet("select rolgroup from hfg_admrole where rolid=" + RolId + "").Tables[0].Rows[0];
            if (dr != null)
                return dr["rolgroup"].ToString();
            else
                return "";
        }
        #endregion

        #region
        #endregion
    }
}
